import request from '@/utils/request'
import type { 
  ApiResponse, 
  PageResponse, 
  NoteLike, 
  NoteFavorite, 
  NoteComment,
  CommentQuery
} from '@/types/api'

// 点赞相关
export function toggleLike(noteId: number): Promise<ApiResponse<{ isLiked: boolean; likeCount: number }>> {
  return request({
    url: '/blog/interaction/like/toggle',
    method: 'post',
    data: { noteId }
  })
}

export function checkLikeStatus(noteId: number): Promise<ApiResponse<boolean>> {
  return request({
    url: '/blog/interaction/like/check',
    method: 'get',
    params: { noteId }
  })
}

export function getUserLikedNotes(pageNum = 1, pageSize = 10): Promise<ApiResponse<PageResponse<NoteLike>>> {
  return request({
    url: '/blog/interaction/like/user',
    method: 'get',
    params: { pageNum, pageSize }
  })
}

// 收藏相关
export function toggleFavorite(noteId: number, folderName?: string): Promise<ApiResponse<{ isFavorited: boolean; favoriteCount: number }>> {
  return request({
    url: '/blog/interaction/favorite/toggle',
    method: 'post',
    data: { noteId, folderName }
  })
}

export function checkFavoriteStatus(noteId: number): Promise<ApiResponse<boolean>> {
  return request({
    url: '/blog/interaction/favorite/check',
    method: 'get',
    params: { noteId }
  })
}

export function getUserFavoriteNotes(pageNum = 1, pageSize = 10, folderName?: string): Promise<ApiResponse<PageResponse<NoteFavorite>>> {
  return request({
    url: '/blog/interaction/favorite/user',
    method: 'get',
    params: { pageNum, pageSize, folderName }
  })
}

export function getUserFavoriteFolders(): Promise<ApiResponse<string[]>> {
  return request({
    url: '/blog/interaction/favorite/folders',
    method: 'get'
  })
}

// 评论相关
export function addComment(data: { noteId: number; content: string; parentId?: number }): Promise<ApiResponse<NoteComment>> {
  return request({
    url: '/blog/interaction/comment/add',
    method: 'post',
    data
  })
}

export function getNoteComments(params: CommentQuery): Promise<ApiResponse<PageResponse<NoteComment>>> {
  return request({
    url: '/blog/interaction/comment/list',
    method: 'get',
    params
  })
}

export function deleteComment(commentId: number): Promise<ApiResponse> {
  return request({
    url: `/blog/interaction/comment/${commentId}`,
    method: 'delete'
  })
}

export function toggleCommentLike(commentId: number): Promise<ApiResponse<{ likeCount: number }>> {
  return request({
    url: '/blog/interaction/comment/like',
    method: 'post',
    data: { commentId }
  })
}

// 获取笔记统计信息
export function getNoteStats(noteId: number): Promise<ApiResponse<{ likeCount: number; favoriteCount: number; commentCount: number }>> {
  return request({
    url: `/blog/interaction/stats/${noteId}`,
    method: 'get'
  })
}

// 函数别名，保持向后兼容
export const toggleNoteLike = toggleLike
export const toggleNoteFavorite = toggleFavorite